package Fly.example.controller;


import Fly.example.common.R;
import Fly.example.pojo.OrderDetail;
import Fly.example.pojo.Orders;
import Fly.example.service.IOrderDetailService;
import Fly.example.service.IOrdersService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *此页面暂时不写，等到时候移动端的做完再写
 * @author 陽Fly
 * @since 2025-06-16
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private IOrdersService ordersService;
    @Autowired
    private IOrderDetailService orderDetailService;

    //后台订单明细查询
    //先这样，后面写完了那个移动端的后再写这个，因为现在没有订单可以查询
    //难以判断写的对错
    @GetMapping("/page")
    public R<Page<Orders>> page(Integer page, Integer pageSize, String number, String beginTime, String endTime) {
        log.info("页数为{}，每页的条数为{}，订单模糊查询{}，开始时间为{}，结束时间为{}",page,pageSize,number,beginTime,endTime);
        Page<Orders> pageinfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(number!=null,Orders::getNumber, number);
        queryWrapper.between(beginTime!=null&&endTime!=null,Orders::getOrderTime, beginTime,endTime);
        ordersService.page(pageinfo, queryWrapper);
        return R.success(pageinfo);
    }

    //移动端的订单列表
    @GetMapping("/userPage")
    public R<Page<Orders>> userPage(HttpSession session, Integer page, Integer pageSize){
        log.info("得到移动端发送的请求，页数为{}，每页的条数为：{}",page,pageSize);
        Object userid = session.getAttribute("user");
        log.info("当前登录的用户id为{}",userid);
        Page<Orders> pageinfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId, userid);
        queryWrapper.orderByDesc(Orders::getOrderTime);
        ordersService.page(pageinfo, queryWrapper);
        return R.success(pageinfo);
    }

    //用户下单
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("用户下单信息为：{}",orders);
        ordersService.submit(orders);
        return R.success("下单成功");
    }

    //修改订单状态
    @PutMapping
    public R<String> setStatus(@RequestBody Orders orders){
        log.info("修改订单状态业务开始…………");
        log.info("接收到数据：{}",orders);
        ordersService.updateById(orders);
        return R.success("修改成功");
    }

    //再来一单的业务
    @PostMapping("/again")
    @Transactional
    public R<String> again(@RequestBody Orders orders){
        log.info("再来一单的数据为：{}",orders);
        Orders orders1 = ordersService.getById(orders.getId());
        BeanUtils.copyProperties(orders1,orders);
        orders.setId(IdWorker.getId());
        orders.setNumber(String.valueOf(IdWorker.getId()));
        orders.setStatus(2);
        orders.setOrderTime(LocalDateTime.now());
        //保存数据到订单表
        log.info(String.valueOf(orders));
        ordersService.save(orders);
        //补充订单明细表
        LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderDetail::getOrderId, orders1.getId());
        List<OrderDetail> orderDetailList = orderDetailService.list(queryWrapper);
        if (orderDetailList != null && orderDetailList.size() > 0) {
            orderDetailList = orderDetailList.stream().map(item->{
                OrderDetail orderDetail = new OrderDetail();
                BeanUtils.copyProperties(item,orderDetail);
                orderDetail.setOrderId(IdWorker.getId());
                orderDetail.setId(IdWorker.getId());
                return orderDetail;
            }).collect(Collectors.toList());
            log.info(String.valueOf(orderDetailList));
            orderDetailService.saveBatch(orderDetailList);
        }
        return R.success("操作成功");
    }
}
